<?php
	include "../database.php";
	
	$id = $_POST["id"];
	$username = $_POST["username"];
	$name = $_POST["first_name"];
	$last_name = $_POST["last_name"];
	$mail = $_POST["email"];
	$pass = $_POST["password"];
	$pass_confirm = $_POST["password_confirm"];
	
	
//	Preverjamo enakost vnesenih gesel 
	if ($pass == $pass_confirm) {
		
		// Poizvedba ce ima ta username drugi uporabnik
		$checkUsernameQuery = "SELECT id,username FROM user WHERE username='$username';";
    	$checkUsernameResult = executeQuery($checkUsernameQuery);
    	
    	$rowsNumber = mysql_num_rows($checkUsernameResult);
    	$checkUsernameResult = mysql_fetch_row($checkUsernameResult);
    	
//    	echo "ECHO ID = $checkUsernameResult[0] USERNAME = $checkUsernameResult[1] <br/>";
//	    echo "ROWS = $rowsNumber";
//   		echo "======================== <br/>";
   		
   		// Poizvedba da dobimo nas trenutni username
   		$sameUsernameQuery = "SELECT username FROM user WHERE id='$id';";
   		$sameUsernameResult = executeQuery($sameUsernameQuery);
   		
   		$rowsNumber2 = mysql_num_rows($sameUsernameResult);
   		$sameUsernameResult = mysql_fetch_row($sameUsernameResult);
    	
//    	echo "ECHO USERNAMEFROMBASE = $sameUsernameResult[0] MYUSERNAME = $username <br/>";
//	    echo "ROWS = $rowsNumber2";
//   		echo "======================== <br/>";
    	
    	// Poizvedba za spreminjanje podatkov
    	$query = "UPDATE user SET username='$username', name='$name', last_name='$last_name', mail='$mail', pass='$pass' WHERE id=$id;";

    	// Ce spreminjamo ostale podatke, username pa ostane enak
		if ($sameUsernameResult[0] == $username ) {  
			executeQuery($query);
			header("location: ../control_panel.php?username=$username");

		// Ce username se ni zaseden
		} elseif($rowsNumber < 1){

			executeQuery($query);
			header("location: ../control_panel.php?username=$username");
		
		// Drugace sporoci, da je username zaseden
		} else {
			
			$id_msg = 101;
			header("location: ../edit_profile.php?msg=$id_msg");
		}
							
	// Drugace sporoci napako za geslo	
	} else {
		
		$id_msg = 102;
		header("location: ../edit_profile.php?msg=$id_msg");
	}	
	
?>
